package uz.edo.app.admin.rolePages;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.admin.AdminPage;
import uz.edo.db.bean.Role;
import uz.edo.db.manager.RoleManager;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/23/12
 * Time: 5:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class RoleListView extends AdminPage{

    private RoleManager manager = new RoleManager();

    public RoleListView() {
        final DataView dataView;
        try {
            List<Role> roles = isPrivileged("su") ? manager.getAll() : manager.getRoles();
            dataView = new DataView("roles",new ListDataProvider(roles),ROWS_PER_PAGE){
                @Override
                protected void populateItem(Item item) {
                    final Role role = (Role)item.getModelObject();
                    final CompoundPropertyModel userModel = new CompoundPropertyModel(role);
                    item.setModel(userModel);
                    item.add(new MultiLineLabel("name"));
                    item.add(new MultiLineLabel("description"));
                    item.add(new MultiLineLabel("privilegesStr"));
                    item.add(new Link("editLink"){
                        @Override
                        public void onClick() {
                            setResponsePage(new RoleEdit(role));
                        }
                    });
                    item.add(new Link("deleteLink"){
                        @Override
                        public void onClick() {
                            try {
                                manager.delete(role);
                                setResponsePage(RoleListView.class);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            };

            add(dataView);
            add(new PagingNavigator("navigator", dataView){
				public boolean isVisible() {
                    return dataView.getPageCount() > 1;
                }
            });

            add(new Link("addLink") {
                @Override
                public void onClick() {
                    goToNew();
                }
            });

            add(new Link("addLink2") {
                @Override
                public void onClick() {
                    goToNew();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void goToNew(){
        setResponsePage(RoleAdd.class);
    }
}
